package com.pr.makfilm.user.pages.school.list.cmp;

import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.button.BootstrapLink;
import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.toolbar.FilterToolbar;
import com.pr.makfilm.domain.School;
import com.pr.makfilm.domain.SchoolType;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.service.ISchoolService;
import com.pr.makfilm.service.ISchoolTypeService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 25.09.13
 * Time: 23:57
 * To change this template use File | Settings | File Templates.
 */
public class SchoolTypeFilterToolbar extends FilterToolbar<SchoolType>
{
   @SpringBean
   private ISchoolTypeService schoolTypeService;

   @SpringBean
   private ISchoolService schoolService;

   private TypeWindow window;

   private AjaxLink<Void> newType;

   private BootstrapLink editType;

   private BootstrapLink deleteType;

   public SchoolTypeFilterToolbar(final AjaxDataTable<SchoolType> table, IModel<SchoolType> model)
   {
      super(table);
      setDefaultModel(model);
      setOutputMarkupId(true);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      window = new TypeWindow("window")
      {
         @Override
         protected void onAfterSubmit(AjaxRequestTarget target)
         {
            refreshTableView(target);
         }
      };
      add(window);
      newType = new AjaxLink<Void>("new")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(true);
            window.show(target);
         }
      };
      add(newType);
      editType = new BootstrapLink("edit")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(false);
            window.setType(getSelected());
            window.show(target);
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(editType);

      deleteType = new BootstrapLink("delete")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            List<School> schools = schoolService.findByType(getSelected());
            if (CollectionUtils.isEmpty(schools))
            {
               schoolTypeService.delete(getSelected());
//               selected = null;
               Notification.success(getString("type.successfully.deleted"));
               refreshTableView(target);
            }
            else
            {
               Notification.error(getString("type.already.in.use"));
            }
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("type.delete.confirm")));
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(deleteType);
   }

   @Override
   public void registerFilters(DataTable<?, ?> table, OpenSearchDescription<SchoolType> osd)
   {
      addFilter(new TextField<String>("name", new PropertyModel<String>(osd, "filter.name")), getString("name"));
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
      target.add(editType);
      target.add(deleteType);
   }

   private SchoolType getSelected()
   {
      return (SchoolType) getDefaultModelObject();
   }
}
